package com.maoyan.quickdevelop.system.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.maoyan.quickdevelop.common.constant.HttpStatus;
import com.maoyan.quickdevelop.common.core.domain.DqSc;
import com.maoyan.quickdevelop.common.core.exception.CustomException;
import com.maoyan.quickdevelop.common.utils.StringUtils;
import com.maoyan.quickdevelop.system.mapper.DqScMapper;
import com.maoyan.quickdevelop.system.service.IDqScService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 猫颜
 * @date 2021/5/27 22:28
 * 状态校验的方法先islogin，再判断权限或身份
 */
@Transactional
@Service
public class IDqScServiceImpl implements IDqScService {

    QueryWrapper<DqSc> queryWrapper = new QueryWrapper<>();
    @Autowired
    private DqScMapper dqScMapper;

    @Override
    public List<DqSc> selectDqSc(int pageNum, int pageSize, DqSc dqSc) {
        PageHelper.startPage(pageNum, pageSize);
        queryWrapper.lambda().eq(StringUtils.isNotNull(dqSc.getScUser()), DqSc::getScUser, dqSc.getScUser());
        List<DqSc> dqScs = dqScMapper.selectList(queryWrapper);
        queryWrapper.clear();
        if (dqScs.isEmpty()) {
            throw new CustomException("未查询到收藏", HttpStatus.NOT_FOUND);
        }
        return dqScs;
    }

    @Override
    public List<DqSc> selectDqSc1(int pageNum, int pageSize, DqSc dqSc) {
        PageHelper.startPage(pageNum, pageSize);
        queryWrapper.lambda().eq(StringUtils.isNotNull(dqSc.getScUser()), DqSc::getScUser, dqSc.getScUser())
                .eq(StringUtils.isNotNull(dqSc.getScArticle()), DqSc::getScArticle, dqSc.getScArticle());
        List<DqSc> dqScs = dqScMapper.selectList(queryWrapper);
        queryWrapper.clear();
        if (dqScs.isEmpty()) {
            throw new CustomException("未查询到收藏", HttpStatus.NOT_FOUND);
        }
        return dqScs;
    }

    @Override
    public int insertDqSc(DqSc dqSc) {
        int insert = dqScMapper.insert(dqSc);
        if (insert <= 0) {
            throw new CustomException("添加失败", HttpStatus.ERROR);
        }
        return insert;
    }

    @Override
    public int deleteDqScById(Long dqScId) {
        DqSc dqSc = dqScMapper.selectById(dqScId);
        if (dqSc == null) {
            throw new CustomException("未查询到收藏", HttpStatus.NOT_FOUND);
        }
        int i = dqScMapper.deleteById(dqScId);
        if (i > 0) {
            return i;
        } else {
            throw new CustomException("删除失败", HttpStatus.ERROR);
        }
    }

    @Override
    public int selectDqScCount(Long articleId) {
        DqSc dqSc = new DqSc();
        dqSc.setScArticle(articleId);
        queryWrapper.lambda().eq(StringUtils.isNotNull(dqSc.getScArticle()), DqSc::getScArticle, dqSc.getScArticle());
        List<DqSc> dqScs = dqScMapper.selectList(queryWrapper);
        queryWrapper.clear();
        return dqScs.size();
    }
}

